package org.longteng.mobileluck.algorithm.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.longteng.common.tool.chinese.ChineseCharBihuaUtil;
import org.longteng.mobileluck.algorithm.bean.ChinesStrokesFortuneTable;
import org.longteng.mobileluck.algorithm.dao.ChinesStrokesFortuneDao;
import org.longteng.mobileluck.algorithm.service.ChinesStrokesFortuneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 姓名笔画数吉祥如意计算service实现
 * <br>这个网不只是笔画测算的，更准：https://xmcs.buyiju.com/
 * @author hymn.com
 */
@Slf4j
@Service
public class ChinesStrokesFortuneServiceImpl implements ChinesStrokesFortuneService {
    private ChinesStrokesFortuneDao chinesStrokesFortuneDao;
    @Autowired
    public void setChinesStrokesFortuneDao(ChinesStrokesFortuneDao chinesStrokesFortuneDao) {
        this.chinesStrokesFortuneDao = chinesStrokesFortuneDao;
    }

    /**
     * 根据姓名笔画数计算出解释批注测吉凶
     *
     * @param chinesName 姓名
     * @return
     */
    @Override
    public ChinesStrokesFortuneTable parseNameLuck(String chinesName) {
        int chinesStrokes = ChineseCharBihuaUtil.getStrStrokeCount(chinesName); // 获取字符串中所有汉字的笔画总和

        return chinesStrokesFortuneDao.getAuspiciousEntity(chinesStrokes);
    }

    /**
     * 根据姓名分字笔画数计算出解释批注测吉凶
     *
     * @param chinesName 姓名
     * @return
     */
    @Override
    public Map<String, ChinesStrokesFortuneTable> parseNameCharLuck(String chinesName) {
        if (StringUtils.isBlank(chinesName)) {
            return null;
        }

        Map<String, ChinesStrokesFortuneTable> nameCharLuckMap = new LinkedHashMap<>();
        char[] words = chinesName.toCharArray();
        int count = 0;
        for (int i = 0; i < words.length; i++) {
            int chinesStrokes =  ChineseCharBihuaUtil.getStrokeCount(words[i]);
            if(log.isDebugEnabled()) {
                log.debug("姓名各字'{}'笔画数据：{}", String.valueOf(words[i]), chinesStrokes);
            }
            if (chinesStrokes > 0) {
                ChinesStrokesFortuneTable chinesStrokesFortune = chinesStrokesFortuneDao.getAuspiciousEntity(chinesStrokes);
                nameCharLuckMap.put(String.valueOf(words[i]), chinesStrokesFortune);
                count += chinesStrokes;
            }
        }
        return nameCharLuckMap;
    }
}
